<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="二维码" prop="qrcodeName">
        <el-input
          v-model="queryParams.qrcodeName"
          placeholder="请输入二维码名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="'/groupchatAutoPull/add'">
          <el-button type="primary"  plain size="mini" icon="el-icon-plus" v-hasPermi="['wework:groupchatAutoPull:add']">
            新增
          </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wework:groupchatAutoPull:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wework:groupchatAutoPull:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wework:groupchatAutoPull:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupchatAutoPullList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="二维码名称" align="center" prop="id" v-if="false"/>
      <el-table-column label="二维码" align="center" prop="qrcodeUrl" >
        <template slot-scope="scope">
          <el-avatar shape="square" :size="120" :src="scope.row.qrcodeUrl"></el-avatar><br/>
          {{scope.row.qrcodeName}}
        </template>
      </el-table-column>
      <el-table-column label="使用成员" align="center" prop="employees">
        <template slot-scope="scope">
          <div v-for="employee in scope.row.employees"><el-tag type="info" size="mini">{{employee.name}}</el-tag></div>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tags">
        <template scope="scope">
          <el-tag v-for="tag in tagsFormat(scope.row)" size="mini">{{tag.name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="客户数 " align="center"/>
      <el-table-column label="群（人数）" align="left" prop="groupchats" width="240px">
        <template slot-scope="scope">
          <div v-for="g in scope.row.groupchats">
            {{g.name}}({{g.memberNum}}/{{g.max}})<br/>
            <el-tag type="info" size="mini">{{g.owner.name}}</el-tag>
          </div>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-download" @click="downloadQrcode(scope.row)">
            下载
          </el-button><br/>
          <router-link :to="'/groupchatAutoPull/edit/'+scope.row.id">
            <el-button type="text" size="mini" icon="el-icon-edit" v-hasPermi="['wework:groupchatAutoPull:edit']">
              修改
            </el-button>
          </router-link><br/>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wework:groupchatAutoPull:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listGroupchatAutoPull, getGroupchatAutoPull, delGroupchatAutoPull, addGroupchatAutoPull, updateGroupchatAutoPull, exportGroupchatAutoPull } from "@/api/wework/groupchatAutoPull";
import Template from "@/views/multitenant/template";
import {listTag} from "@/api/wework/tag";

export default {
  name: "GroupchatAutoPull",
  components: {
    Template
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自动拉群表格数据
      groupchatAutoPullList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        qrcodeName: undefined,
      },
    };
  },
  created() {
    this.getList();
    listTag().then(response => {
      this.tagGroups = response.data;
    });
  },
  methods: {
    /** 查询自动拉群列表 */
    getList() {
      this.loading = true;
      listGroupchatAutoPull(this.queryParams).then(response => {
        this.groupchatAutoPullList = response.rows;
        this.groupchatAutoPullList.forEach(autopull=>{
          autopull.groupchats = JSON.parse(autopull.groupchats);
          autopull.employees = JSON.parse(autopull.employees);
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 客户群状态字典翻译
    tagsFormat(row, column) {
      let tags = JSON.parse(row.tags)
      if (tags == null){
        return
      }
      let tagList = tags.map(t=>this.selectTag(t[0],t[1]))
      return tagList;
    },
    selectTag(groupId,tagId){
      let groupIndex = this.tagGroups.findIndex(g=>g.id===groupId);
      let tagList = this.tagGroups[groupIndex].tagList;
      let index = tagList.findIndex(t=>t.id===tagId);
      return tagList[index]
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        corpId: undefined,
        qrcodeName: undefined,
        qrcodeUrl: undefined,
        wxConfigId: undefined,
        isVerified: undefined,
        leadingWords: undefined,
        tags: undefined,
        employees: undefined,
        groupchats: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自动拉群";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getGroupchatAutoPull(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改自动拉群";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroupchatAutoPull(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroupchatAutoPull(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除自动拉群编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delGroupchatAutoPull(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有自动拉群数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportGroupchatAutoPull(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    downloadQrcode(row){
      const aTag = document.createElement('a');
      aTag.href = row.qrcodeUrl;
      aTag.click();
    }
  }
};
</script>
